﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Modules.PowerShell.Args;
using System.Text;
using DotNetNuke.Modules.PowerShell.Utils;
using DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Portals;

namespace DotNetNuke.Modules.PowerShell.Contexts
{
	public abstract class BaseContext : IShellContext
	{
		private StringBuilder _resultBuilder = new StringBuilder();

		protected bool IsValid { get; set; }

		public string Result
		{
			get { return _resultBuilder.ToString(); }
		}

		public UserInfo CurrentUser
		{
			get { return UserController.GetCurrentUserInfo(); }
		}

		public PortalSettings CurrentPortalSettings
		{
			get { return PortalSettings.Current; }
		}

		public virtual void Process(IContextArgs args)
		{
			if ((args.Action.Equals("?") || args.Action.Equals("/?")))
			{
				IsValid = false;
				var help = ContextHelper.CreateContext(args.ContextName).Help;
				_resultBuilder.Append(help);
			}
			else
			{
				IsValid = true;
			}
		}

		protected void AddResult(string result)
		{
			_resultBuilder.AppendLine(result);
		}

		protected void AddResult(string format, params object[] parameters)
		{
			AddResult(string.Format(format, parameters));
		}

		public abstract string Help { get; }
	}
}